package tech.aistar.controller.userVerified;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import tech.aistar.config.GuidelInterceptor;
import tech.aistar.entity.User.CodeStatus;
import tech.aistar.entity.User.UserResult;
import tech.aistar.entity.authordis.UserTable;
import tech.aistar.entity.daoyou.GuideCodeStatus;
import tech.aistar.entity.daoyou.GuideResult;
import tech.aistar.entity.daoyou.UserIdcard;
import tech.aistar.exception.LogginException;
import tech.aistar.service.userVerifiedService.IUserVerifiedService;
import tech.aistar.util.verified.PhontoApi;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Created by lenovo on 2019/10/16.
 */
@Controller
public class userVerified {

    @Autowired
    private IUserVerifiedService userVerifiedService;


    @RequestMapping("/userverified")
    public String returnLogin(Model model, HttpServletRequest request) {
//

        HttpSession session = request.getSession();
        UserTable userTable = (UserTable) session.getAttribute("userId");
        model.addAttribute("userTable", userTable);
        return "register/userVerified";
    }


    @RequestMapping(value = "/addUser1")
    @ResponseBody
    public Map addUser(HttpServletRequest request, MultipartFile myfile3) throws Exception {

        Map<String, String> map = new HashMap<>();

        //使用UUID给图片重命名，并去掉四个“-”
        String name = UUID.randomUUID().toString().replaceAll("-", "");
        //获取文件的扩展名
        String ext = FilenameUtils.getExtension(myfile3.getOriginalFilename());
        //设置图片上传路径
        String url = request.getSession().getServletContext().getRealPath("/imgs/upload");
        //以绝对路径保存重名命后的图片
        myfile3.transferTo(new File(url + "/" + name + "." + ext));
        //拼接绝对路径到页面,回显数据
        String imgUrl = ("imgs/upload/" + name + "." + ext);

        //获取最后的路径,验证身份信息
        String path = request.getSession().getServletContext().getRealPath("/imgs/upload/" + name + "." + ext);


        //放入map数据回显
        map.put("path", path);
        map.put("imgUrl", imgUrl);
        if (imgUrl != null) {
            return map;
        } else {
            return map;
        }
    }


    @RequestMapping("/verified1")
    @ResponseBody
    public GuideResult verified(@RequestParam String paths, Model model, HttpSession session) {
        System.err.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");


        HashMap<String, String> map = null;
        try {
            map = PhontoApi.checkPhone(paths);
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println(map);
        String idcard = map.get("IDCard");
        System.out.println(idcard);
        UserTable user = userVerifiedService.findUser(idcard);
        System.out.println(user);
        if (map.size() > 0) {
            if (null != user) {
                return new GuideResult(GuideCodeStatus.OK, false, "该用户已经实名认证");
            } else {
                UserTable userTable = (UserTable) session.getAttribute("userId");
                Integer userid = userTable.getId();
                userVerifiedService.saveUser(map.get("birth"), map.get("IDCard"), userid);
                return new GuideResult(GuideCodeStatus.OK, true, "信息正确", map);
            }
        } else {
            return new GuideResult(GuideCodeStatus.OK, false, "图片格式错误重新上传");
        }


    }


}
